import time
import datetime
import json
from flask import jsonify, request
from .read_model import *
from src.utils.date_translate import from_str_to_date


# 用户信息
class OperaUser(object):
    @staticmethod
    def put(put_data, requst_json):
        put_data.id = int(requst_json.get('id'))
        put_data.name = requst_json.get('name')
        put_data.password_hash = requst_json.get('password_hash')
        put_data.registered_on = requst_json.get('registered_on')

        db.session.commit()
        return 'update success', 204

    @staticmethod
    def post(requst_json):
        name = requst_json.get('name')
        password_hash = requst_json.get('password_hash')

        task = UserInfo(name=name, password_hash=password_hash, registered_on=int(time.time()))
        db.session.add(task)
        db.session.commit()
        return 'create success', 201

    def post_and_put(self, request_json=None):
        if request_json is None:
            request_json = request.json
        if isinstance(request_json, str):
            request_json = json.loads(request_json)

        if not request_json:
            return 'Has no data: {}'.format(request_json), 400
        else:
            table_data = UserInfo.query.filter_by(id=int(request_json.get('id'))).first()
            if table_data:
                self.put(put_data=table_data, requst_json=request_json)
                return 'put success', 204
            else:
                self.post(requst_json=request_json)
                return 'post data success', 201


# 操作账户表
class OperaDim(object):
    @staticmethod
    def get(category_code, name=None):
        if name:
            table_info = CommonCode.query.filter_by(category_code=category_code, name=name, is_valid=1).first()
            return True if table_info else False
        table_info = read_model_common_code(category_code=category_code)
        if table_info:
            json_data = {
                'code': 200,
                'data': table_info
            }
            return jsonify(json_data)

    @staticmethod
    def post(category_code, json_data):
        name = json_data.get('name')

        task = CommonCode(category_code=category_code, name=name, is_valid=1)

        db.session.add(task)
        db.session.commit()
        return 'post success', 201


# 账户表
class OperaAccountType(object):
    @staticmethod
    def get(user_id, name=None):
        if name:
            table_info = AccountType.query.filter_by(user_id=user_id, name=name, is_valid=1).first()
            return True if table_info else False
        if user_id and user_id != 'None':
            table_info = read_model_account_type(user_id=int(user_id))

            json_data = {
                'code': 200,
                'data': table_info
            }
            return jsonify(json_data)

    @staticmethod
    def post(json_data):
        type_id = json_data.get('type_id')
        account_name = json_data.get('account_name')
        user_id = json_data.get('user_id')

        task = AccountType(type_id=type_id, account_name=account_name, user_id=user_id, is_valid=1)

        db.session.add(task)
        db.session.commit()
        return 'post success', 201


# 流水账
class OperaAccountInfo(object):
    @staticmethod
    def get(user_id, date=None, money=None, income_expense_id=None, account_id=None):
        if date and money and income_expense_id and account_id:
            query = AccountInfo.query.filter_by(user_id=user_id).filter_by(date=date).filter_by(money=money). \
                filter_by(income_expense_id=income_expense_id).filter_by(account_id=account_id).\
                filter_by(is_valid=1).first()
            return True if query else False

    @staticmethod
    def post(json_data):
        date = json_data.get('date')
        money = json_data.get('money')
        income_expense_class_id = json_data.get('income_expense_class_id')
        income_expense_id = json_data.get('income_expense_id')
        income_expense_type_id = json_data.get('income_expense_type_id')
        account_id = json_data.get('account_id')
        account_class_id = json_data.get('account_class_id')
        pay_channel_id = json_data.get('pay_channel_id') 
        remark = json_data.get('remark')
        user_id = json_data.get('user_id')

        task = AccountInfo(date=date, money=money, income_expense_id=income_expense_id,
                           income_expense_class_id=income_expense_class_id, account_class_id=account_class_id,
                           income_expense_type_id=income_expense_type_id, account_id=account_id, remark=remark,
                           user_id=user_id, pay_channel_id=pay_channel_id, is_valid=1)

        db.session.add(task)
        db.session.commit()
        return 'post success', 201

    @staticmethod
    def update(id):
        """
        退款
        :return:
        """
        db.session.query(AccountInfo).filter(AccountInfo.id == id).update({"is_valid": 0})
        db.session.commit()
        return 'update success', 201



# 维度表
opera_dim = OperaDim()
# 用户信息
opera_user = OperaUser()
# 账户表
opera_account_type = OperaAccountType()
# 流水账
opera_account_info = OperaAccountInfo()
